<?php
/**
 * Created by PhpStorm.
 * User: Inhere
 * Date: 2015/2/6
 * Use : ...
 * File: SessionOperator.php
 */

namespace ulue\parts\session\operator;


class SessionOperator implements InterfaceSessionOperator
{

    /**
     * 设置
     * @param $key
     * @param $value
     * @return $this
     */
    public function set($key, $value)
    {
        if (is_string($key) && $key)
        {
            $_SESSION[$key] = $value;
        }

        return $this;
    }

    public function mset(array $data)
    {
        foreach ($data as $key => $value)
        {
            if (is_string($key) && $key)
            {
                $_SESSION[$key] = $value;
            }
        }

        return $this;
    }

    /**
     * @param $key
     * @param null $default
     * @return null
     */
    public function get($key, $default=null)
    {
        return isset($_SESSION[$key]) ? $_SESSION[$key] : $default;
    }

    /**
     * 获取多个
     * @param $keys
     * @return array
     */
    public function mget(array $keys)
    {
        $vs = [];

        foreach ($keys as $key)
        {
            $vs[] = $this->get($key);
        }

        return $vs;
    }

    public function exists($key)
    {
        return isset($_SESSION[$key]);
    }

    public function remove($key)
    {
        if(isset($_SESSION[$key]))
        {
            unset($_SESSION[$key]);
        }
    }

    public function all()
    {
        return $_SESSION;
    }

    public function clear()
    {
        $_SESSION = [];
    }

}// end class OperationalData